package com.tgz.websocket;


import com.tgz.config.MyVar;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.ChannelRegistration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;


/**
 * @Author JCccc
 * @Description   EnableWebSocketMessageBroker-注解开启STOMP协议来传输基于代理的消息，此时控制器支持使用@MessageMapping
 * @Date 2021/6/30 8:53
 */

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

    @Autowired
    private MyVar myVar;
//    @Autowired
//    private AuthChannelInterceptor authChannelInterceptor;

    /**
     * websocket权限拦截器方式[全局]
     *
     * @param registration
     */
//    @Override
//    public void configureClientInboundChannel(ChannelRegistration registration) {
//        registration.interceptors(authChannelInterceptor);
//    }

    @Override
    public void configureMessageBroker(MessageBrokerRegistry config) {
        //用来广播前缀、一对一
        config.enableSimpleBroker(myVar.getTopic(),myVar.getTopicUser());
    }

    /**
     * 开放节点
     * @param registry
     */
    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        //注册STOMP的endpoint，分别用于广播
        //广播
        registry.addEndpoint(myVar.getEndpoint()).setAllowedOrigins("*").withSockJS();
        //一对一
        registry.addEndpoint(myVar.getEndpointUser()).setAllowedOrigins("*").withSockJS();
    }


}